﻿using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using Lamex.Base;
using Lamex.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.Library.CommonClass
{
  public  class FileImoprtHelper
    {
        public static bool ImportFormTypeList(GridView View,string FormTypeName)
        {
            try
            {
                if(FormTypeName=="排产系数")
                {
                    string fileName = "ZP11_UP.TXT";
                    string path = @"C:\SAPTEMP\";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    path = path + fileName;
                    StringBuilder builder = new StringBuilder();
                    //builder.Append("部件编号" + "\t" + "工序组" + "\t" + "计数器" + "\t" + "计数器描述" + "\t" + "序号" + "\t" + "工序代码" + "\t" + "工序工时(S)" + "\t" + "并行标识" + "\r\n");

                    for(int i = 0 ; i < View.RowCount;i++)
                    {
                        builder.Append(View.GetRowCellDisplayText(i, "PROCESS_CODE") + "\t" + View.GetRowCellDisplayText(i, "PROCESS_NAME") + "\t" + View.GetRowCellDisplayText(i, "SCHEDULE_FACTORY_CODE") + "\t" + View.GetRowCellDisplayText(i, "WORK_SHIFT_HOURS") + "\t" + View.GetRowCellDisplayText(i, "SCHEDULE_FACTOR_MAX") + "\t" + View.GetRowCellDisplayText(i, "SCHEDULE_FACTOR_STD") + "\t" + View.GetRowCellDisplayText(i, "PROCESS_TYPE") + "\t" + View.GetRowCellDisplayText(i, "PROCESS_CAPACITY") + "\t" + View.GetRowCellDisplayText(i, "SPLIT_NOS") + "\r\n");

                    }
                    using (System.IO.FileStream file = new System.IO.FileStream(path, System.IO.FileMode.Create, System.IO.FileAccess.Write))
                    {
                        using (System.IO.TextWriter text = new System.IO.StreamWriter(file, System.Text.Encoding.Default))
                        {
                            text.Write(builder.ToString());
                        }
                    }
                    SplashScreenTool.CloseSplashScreen();
                    XtraMessageBox.Show("数据导出成功,请登陆SAP执行ZP11进行上载！");
                    return true;
               
                }
                else if (FormTypeName == "成本系数")
                {
                    string fileName = "ZPZT_UP.TXT";
                    string path = @"C:\SAPTEMP\";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    path = path + fileName;
                    StringBuilder builder = new StringBuilder(); 
                    builder.Append("工作中心" + "\t" + "Type" + "\t" + "描述" + "\t" + "工作中心类别" + "\t" + "数量" + "\t" + "单机定员" + "\t" + "管理系数" + "\t" + "生效日期" + "\r\n");

                    for (int i = 0; i < View.RowCount; i++)
                    {
                        decimal value = 0;
                        if(View.GetRowCellDisplayText(i, "PROCESS_TYPE").ToString()=="设备")
                        {
                            if (View.GetRowCellDisplayText(i, "EQP_QUANTITY") != null)
                                value = decimal.Parse(View.GetRowCellDisplayText(i, "EQP_QUANTITY").ToString());
                        }
                        else
                        {
                            if (View.GetRowCellDisplayText(i, "WORKERS_PER_WORKCENTER") != null)
                                value = decimal.Parse(View.GetRowCellDisplayText(i, "WORKERS_PER_WORKCENTER").ToString());
                        }
                        builder.Append(View.GetRowCellDisplayText(i, "PROCESS_CODE") + "\t" + View.GetRowCellDisplayText(i, "COST_CODE") + "\t" + View.GetRowCellDisplayText(i, "PROCESS_NAME") + "\t" + View.GetRowCellDisplayText(i, "PROCESS_TYPE") + "\t" + value + "\t" + View.GetRowCellDisplayText(i, "WORKERS_PER_EQP") + "\t" + View.GetRowCellDisplayText(i, "COST_FACTOR") + "\t" + Convert.ToDateTime(View.GetRowCellDisplayText(i, "EFFECTIVE_DATE")).ToString("yyyy/MM/dd") + "\t" + "\r\n");

                    }
                    using (System.IO.FileStream file = new System.IO.FileStream(path, System.IO.FileMode.Create, System.IO.FileAccess.Write))
                    {
                        using (System.IO.TextWriter text = new System.IO.StreamWriter(file, System.Text.Encoding.Default))
                        {
                            text.Write(builder.ToString());
                        }
                    }
                    SplashScreenTool.CloseSplashScreen();
                    XtraMessageBox.Show("数据导出成功,请登陆SAP执行ZPZT进行上载！");
                    return true;
                }
                return true;
            }
            catch(Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
                return false;
            }
        }
        /// <summary>
        /// QC导出报表
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static bool ImorpQCTypeList(DataTable dt,string fileName)
        {
            try
            {
                int iRow = dt.Rows.Count;
                int iMod = iRow % 3;
                int iDiv = iRow / 3;
                
                DataTable dttemp = CreateQCDataTable();
                int iFlag = 1;
                if (iMod==0)
                {
                    for(int i = 0;i <= dt.Rows.Count; i++)
                    {
                        if (iFlag <= iDiv)
                        {
                            DataRow dr = dttemp.NewRow();
                            dr["作工站"] = dt.Rows[i]["STATION_NAME"].ToString();
                            dr["工位"] = dt.Rows[i]["LOCATION_CODE"].ToString();
                            dr["目前状态"] = "已完成";
                            dr["停机时长(秒)"] = int.Parse(dt.Rows[i + 1]["TIME_LENGTH"].ToString()) + int.Parse(dt.Rows[i + 2]["TIME_LENGTH"].ToString());
                            dr["响应时长(秒)"] = int.Parse(dt.Rows[i + 1]["TIME_LENGTH"].ToString());
                            dr["处理时长(秒)"] = int.Parse(dt.Rows[i + 2]["TIME_LENGTH"].ToString());
                            dr["品质报告时间"] = dt.Rows[i]["SCAN_TIME"].ToString();
                            dr["开始处理时间"] = dt.Rows[i + 1]["SCAN_TIME"].ToString();
                            dr["结束处理时间"] = dt.Rows[i + 2]["SCAN_TIME"].ToString();
                            dr["影响数量"] = dt.Rows[i + 2]["SCAN_QTY"].ToString();
                            dr["影响品质内容"] = dt.Rows[i + 2]["BARCODE_DESC"].ToString();
                            dr["扫描人"] = dt.Rows[i + 2]["SCANNER_NO"].ToString();
                            dttemp.Rows.Add(dr);
                            if (iFlag == iDiv)//当相等时说明次数刚好用完补充余数条目
                            {
                                break;
                            }
                            iFlag++;
                            i = i + 2;
                        }
                    }
                }
                else
                {
                    
                    
                    for (int i = 0; i <= dt.Rows.Count; i++)
                    {
                        if (iFlag <=iDiv)
                        {
                            DataRow dr = dttemp.NewRow();
                            dr["工作站"] = dt.Rows[i]["STATION_NAME"].ToString();
                            dr["工位"] = dt.Rows[i]["LOCATION_CODE"].ToString();
                            dr["目前状态"] = "已完成";
                            dr["停机时长(秒)"] = int.Parse(dt.Rows[i + 1]["TIME_LENGTH"].ToString()) + int.Parse(dt.Rows[i + 2]["TIME_LENGTH"].ToString());
                            dr["响应时长(秒)"] = int.Parse(dt.Rows[i + 1]["TIME_LENGTH"].ToString());
                            dr["处理时长(秒)"] = int.Parse(dt.Rows[i + 2]["TIME_LENGTH"].ToString());
                            dr["品质报告时间"] = dt.Rows[i]["SCAN_TIME"].ToString();
                            dr["开始处理时间"] = dt.Rows[i + 1]["SCAN_TIME"].ToString();
                            dr["结束处理时间"] = dt.Rows[i + 2]["SCAN_TIME"].ToString();
                            dr["影响数量"] = dt.Rows[i + 2]["SCAN_QTY"].ToString();
                            dr["影响品质内容"] = dt.Rows[i + 2]["BARCODE_DESC"].ToString();
                            dr["扫描人"] = dt.Rows[i + 2]["SCANNER_NO"].ToString();
                            dttemp.Rows.Add(dr);
                            if(iFlag==iDiv)//当相等时说明次数刚好用完补充余数条目
                            {
                                DataRow drsub = dttemp.NewRow();
                                drsub["工作站"] = dt.Rows[i]["STATION_NAME"].ToString();
                                drsub["工位"] = dt.Rows[i]["LOCATION_CODE"].ToString();
                                drsub["目前状态"] = "处理中";
                                drsub["停机时长(秒)"] = "" ;
                                drsub["响应时长(秒)"] = "";
                                drsub["处理时长(秒)"] = "";
                                drsub["品质报告时间"] = "";
                                drsub["开始处理时间"] = "";
                                drsub["结束处理时间"] = "";
                                drsub["影响数量"] = "";
                                drsub["影响品质内容"] = "";
                                drsub["扫描人"] = "";
                                dttemp.Rows.Add(drsub);
                                break;
                            }
                            iFlag++;
                            i = i + 2;
                        }
                    }
                }
                
                Lamex.Common.ExecelHelper.DataTableToExcel(dttemp, fileName);
                return true;
             }
            catch(Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// EQ导出报表
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static bool ImorpEQTypeList(DataTable dt, string fileName)
        {
            try
            {
                int iRow = dt.Rows.Count;
                int iMod = iRow % 4;
                int iDiv = iRow / 4;

                DataTable dttemp = CreateEQDataTable();
                int iFlag = 1;
                if (iMod == 0)
                {
                    for (int i = 0; i <= dt.Rows.Count; i++)
                    {
                        if (iFlag <= iDiv)
                        {
                            DataRow dr = dttemp.NewRow();
                            dr["工作站"] = dt.Rows[i]["STATION_NAME"].ToString();
                            dr["工位"] = dt.Rows[i]["LOCATION_CODE"].ToString();
                            dr["目前状态"] = "已完成";
                            dr["停机时长(秒)"] = int.Parse(dt.Rows[i + 1]["TIME_LENGTH"].ToString()) + int.Parse(dt.Rows[i + 3]["TIME_LENGTH"].ToString());
                            dr["响应时长(秒)"] = int.Parse(dt.Rows[i + 1]["TIME_LENGTH"].ToString());
                            dr["待料时长(秒)"] = 0;
                            dr["维修时长(秒)"] = int.Parse(dt.Rows[i + 3]["TIME_LENGTH"].ToString()); ;
                            dr["故障报告时间"] = dt.Rows[i + 1]["SCAN_TIME"].ToString();
                            dr["故障类别"] = dt.Rows[i]["BARCODE_DESC"].ToString();
                            dr["维修人员"] = dt.Rows[i + 1]["SCANNER_NO"].ToString();
                            dr["维修开始时间"] = dt.Rows[i]["SCAN_TIME"].ToString();
                            dr["维修内容"] = dt.Rows[i + 3]["BARCODE_DESC"].ToString();
                            dr["维修结束时间"] = dt.Rows[i + 3]["SCAN_TIME"].ToString();
                            dttemp.Rows.Add(dr);
                            if (iFlag == iDiv)//当相等时说明次数刚好用完补充余数条目
                            {
                                break;
                            }
                            iFlag++;
                            i = i + 3;
                        }
                    }
                }
                else
                {


                    for (int i = 0; i <= dt.Rows.Count; i++)
                    {
                        if (iFlag <= iDiv)
                        {
                            DataRow dr = dttemp.NewRow();
                            dr["工作站"] = dt.Rows[i]["STATION_NAME"].ToString();
                            dr["工位"] = dt.Rows[i]["LOCATION_CODE"].ToString();
                            dr["目前状态"] = "已完成";
                            dr["停机时长(秒)"] = int.Parse(dt.Rows[i + 1]["TIME_LENGTH"].ToString()) + int.Parse(dt.Rows[i + 3]["TIME_LENGTH"].ToString());
                            dr["响应时长(秒)"] = int.Parse(dt.Rows[i + 1]["TIME_LENGTH"].ToString());
                            dr["待料时长(秒)"] = 0;
                            dr["维修时长(秒)"] = int.Parse(dt.Rows[i + 3]["TIME_LENGTH"].ToString()); ;
                            dr["故障报告时间"] = dt.Rows[i + 1]["SCAN_TIME"].ToString();
                            dr["故障类别"] = dt.Rows[i]["BARCODE_DESC"].ToString();
                            dr["维修人员"] = dt.Rows[i + 1]["SCANNER_NO"].ToString();
                            dr["维修开始时间"] = dt.Rows[i]["SCAN_TIME"].ToString();
                            dr["维修内容"] = dt.Rows[i + 3]["BARCODE_DESC"].ToString();
                            dr["维修结束时间"] = dt.Rows[i + 3]["SCAN_TIME"].ToString();
                            dttemp.Rows.Add(dr);
                            if (iFlag == iDiv)//当相等时说明次数刚好用完补充余数条目
                            {
                                DataRow drsub = dttemp.NewRow();
                                drsub["工作站"] = dt.Rows[i]["STATION_NAME"].ToString();
                                drsub["工位"] = dt.Rows[i]["LOCATION_CODE"].ToString();
                                drsub["目前状态"] = "处理中";
                                drsub["停机时长(秒)"] = "";
                                drsub["响应时长(秒)"] = "";
                                drsub["处理时长(秒)"] = "";
                                drsub["品质报告时间"] = "";
                                drsub["开始处理时间"] = "";
                                drsub["结束处理时间"] = "";
                                drsub["影响数量"] = "";
                                drsub["影响品质内容"] = "";
                                drsub["扫描人"] = "";
                                dttemp.Rows.Add(drsub);
                                break;
                            }
                            iFlag++;
                            i = i + 3;
                        }
                    }
                }

                Lamex.Common.ExecelHelper.DataTableToExcel(dttemp, fileName);
                return true;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
                return false;
            }
        }

        private static DataTable CreateQCDataTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("工作站");
            dt.Columns.Add("工位");
            dt.Columns.Add("目前状态");
            dt.Columns.Add("停机时长(秒)");
            dt.Columns.Add("响应时长(秒)");
            dt.Columns.Add("处理时长(秒)");
            dt.Columns.Add("品质报告时间");
            dt.Columns.Add("开始处理时间");
            dt.Columns.Add("结束处理时间");
            dt.Columns.Add("影响数量");
            dt.Columns.Add("影响品质内容");
            dt.Columns.Add("扫描人");
            return dt;
        }
        private static DataTable CreateEQDataTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("工作站");
            dt.Columns.Add("设备名称或编号");
            dt.Columns.Add("设备状态");
            dt.Columns.Add("停机时长(秒)");
            dt.Columns.Add("响应时长(秒)");
            dt.Columns.Add("待料时长(秒)");
            dt.Columns.Add("维修时长(秒)");
            dt.Columns.Add("故障报告时间");
            dt.Columns.Add("故障类别");
            dt.Columns.Add("维修人员");
            dt.Columns.Add("维修开始时间");
            dt.Columns.Add("维修内容");
            dt.Columns.Add("维修结束时间");
         
            return dt;
        }


        private void FindExcelToDataBase(string sourcePath, string targetPath, List<FieldNameInfo> list, string tableName)
        {
            //DataTable dttemp = new DataTable();
            //dttemp.TableName = tableName;
            //foreach (FieldNameInfo info in list)
            //{
            //    dttemp.Columns.Add(info.FieldName);
            //}
            DataTable dttemp = Lamex.Base.BaseBuildSql.GetDataTableBySql(" select＊from "+tableName+ " where 1!=1 ");
            FileInfo[] files = new DirectoryInfo(sourcePath).GetFiles().Where(p => (p.Extension.ToLower() == ".xlsx") && p.Extension.ToLower() != ".db").ToArray();
            if (files.Count() != 0)
            {
                foreach (FileInfo file in files)
                {
                    DataTable dt = Lamex.Common.ExecelHelper.ExcelConvertDataTable(0, true, file.FullName);
                    if (dt.Rows.Count != 0)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            foreach (DataColumn col in dt.Columns)
                            {
                                DataRow dtrow = dttemp.NewRow();
                                foreach (FieldNameInfo info in list)
                                {
                                    if (col.ColumnName == info.FieldName)//比较两个字段名称
                                    {
                                        dtrow[col.ColumnName] = info;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
